<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>文章分类</span>
        <el-button type="primary" @click="flag = true">添加分类</el-button>
      </div>
      <el-table :data="list" style="width: 100%" border stripe>
        <el-table-column label="id" width="180" prop="id"> </el-table-column>
        <el-table-column label="分类名称" width="180" prop="cate_name"> </el-table-column>
        <el-table-column label="分类别名" prop="cate_alias"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="showedit(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="del(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog title="分类列表" :visible.sync="flag" :before-close="cancel">
      <el-form :model="updata" :rules="rules" ref="addform">
        <el-form-item label="分类名称" label-width="90px" prop="cate_name">
          <el-input autocomplete="off" v-model="updata.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" label-width="90px" prop="cate_alias">
          <el-input autocomplete="off" v-model="updata.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="addData">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="修改分类" :visible.sync="editflag" :before-close="canceledit">
      <el-form ref="editform" :model="editdata" :rules="rules">
        <el-form-item label="分类名称" label-width="90px" prop="cate_name">
          <el-input autocomplete="off" v-model="editdata.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" label-width="90px" prop="cate_alias">
          <el-input autocomplete="off" v-model="editdata.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="canceledit">取 消</el-button>
        <el-button type="primary" @click="editdatacate">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getCateAPI, upCataAPI, deleteCategoryAPI, updataCategoryAPI } from '@/api/category'

export default {
  name: 'CateList',
  data() {
    return {
      list: [],
      editdata: {},
      updata: {
        cate_name: '',
        cate_alias: ''
      },
      flag: false,
      editflag: false,
      rules: {
        cate_name: [
          { required: true, message: '分类名称必填', trigger: 'blur' },
          { min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '分类别名必填', trigger: 'blur' },
          {
            pattern: /^[a-zA-Z0-9]{1,15}$/,
            message: '只能是长度1~15位的字母数字组合',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    async getCategory() {
      const { data: res } = await getCateAPI()
      if (res.code === 0) {
        console.log(res)
        this.list = res.data
      }
    },
    addData() {
      this.$refs.addform.validate(async valid => {
        if (!valid) return
        const { data: res } = await upCataAPI(this.updata)
        if (res.code === 0) {
          this.$message.success(res.message)
          this.$refs.addform.resetFields()
          this.flag = false
          this.getCategory()
        } else {
          this.$message.error(res.message)
        }
      })
    },
    cancel() {
      this.flag = false

      this.$refs.addform.resetFields()
    },
    canceledit() {
      this.editflag = false
    },
    del(id) {
      this.$confirm('确定要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        callback: async r => {
          if (r === 'confirm') {
            const { data: res } = await deleteCategoryAPI(id)
            if (res.code === 0) {
              this.$message.success(res.message)
              this.getCategory()
            }
          }
        }
      })
    },
    showedit(row) {
      this.editflag = true
      this.editdata = { ...row }
    },
    editdatacate() {
      this.$refs.editform.validate(async valid => {
        if (!valid) return
        const { data: res } = await updataCategoryAPI(this.editdata)
        if (res.code === 0) {
          this.$message.success(res.message)
          this.editflag = false
          this.getCategory()
        } else {
          this.$message.error(res.message)
        }
      })
    }
  },
  created() {
    this.getCategory()
  }
}
</script>

<style lang="less" scoped>
.clearfix {
  display: flex;
  justify-content: space-between;
}
</style>
